'use strict';

const BaseController = require('./base');
class OrderController extends BaseController {
  constructor(ctx) {
    super(ctx, 'order');
  }

  /**
   * 获取订单详情
   */
  async detail() {
    const { ctx } = this;
    const { id } = ctx.query;
    try {
      const result = await this.ctx.service[this.serviceName].detail({ id });
      ctx.success(result);
    } catch (error) {
      ctx.error(400, error.message);
    }
  }

  /**
   * 取消订单
   */
  async cancel() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    try {
      await this.ctx.service[this.serviceName].cancel({ id });
      ctx.success();
    } catch (error) {
      ctx.error(400, error.message);
    }
  }

  /**
   * 申请退款
   */
  async refund() {
    const { ctx } = this;
    const { id, refund_reason } = ctx.request.body;
    try {
      await this.ctx.service[this.serviceName].refund({
        id,
        refund_reason,
      });
      ctx.success();
    } catch (error) {
      ctx.error(400, error.message);
    }
  }

  /**
   * 处理退款
   */
  async refundConfirm() {
    const { ctx } = this;
    try {
      await this.ctx.service[this.serviceName].refundConfirm(ctx.request.body);
      ctx.success();
    } catch (error) {
      ctx.error(400, error.message);
    }
  }
  /**
   * 删除订单
   */
  async delete() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    try {
      await this.ctx.service[this.serviceName].delete({ id });
      ctx.success();
    } catch (error) {
      ctx.error(400, error.message);
    }
  }
}

module.exports = OrderController;
